﻿using MediatR;
using ProductionLine.API.Application.Command.Car;
using ProductionLine.Domain.Car;
using ProductionLine.Domain.ProductLine;
using ProductionLine.Domain.Site;
using ProductionLine.ErrorCode;
using ProductionLine.Extensions;
using ProductionLine.Infrastructure.Interface;

namespace ProductionLine.API.Application.Handler.Car
{
    public class GetSiteHandler : IRequestHandler<GetSiteCommand, APIResult<List<SiteDTO>>>
    {
        IBaseRepository<SiteModel> siteRepo;
        IBaseRepository<CarModel> carRepo;
        IBaseRepository<ProductLineModel> proRepo;

        public GetSiteHandler(IBaseRepository<SiteModel> siteRepo, IBaseRepository<CarModel> carRepo, IBaseRepository<ProductLineModel> proRepo)
        {
            this.siteRepo = siteRepo;
            this.carRepo = carRepo;
            this.proRepo = proRepo;
        }

        public Task<APIResult<List<SiteDTO>>> Handle(GetSiteCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<SiteDTO>> res = new APIResult<List<SiteDTO>>();
            var sites=siteRepo.GetAll().Where(c=>!c.IsDel);
            var cars=carRepo.GetAll().Where(c=>!c.IsDel);
            var pros=proRepo.GetAll().Where(c=>!c.IsDel);
            var linq = from a in sites
                       join b in cars on a.CarId equals b.CarId
                       join c in pros on a.LineId equals c.LineId
                       select new SiteDTO
                       {
                           SiteId = a.SiteId,
                           SiteCode=a.SiteCode,
                           SiteName=a.SiteName,
                           SiteType=a.SiteType,
                           SiteLocal=a.SiteLocal,
                           CarId=b.CarId,
                           CarName=b.CarName,
                           LineId=c.LineId,
                           LineName=c.LineName,
                           ProcessId=a.ProcessId
                       };
            res.Code = APIEnum.Success;
            res.Data = linq.ToList();
            res.Msg = "查询站点信息成功";
            return Task.FromResult(res);
        }
    }
}
